package com.maxiaowei.controller;

import com.maxiaowei.pojo.Items;
import com.maxiaowei.pojo.ItemsImg;
import com.maxiaowei.pojo.ItemsParam;
import com.maxiaowei.pojo.ItemsSpec;
import com.maxiaowei.pojo.vo.CommentLevelCountVO;
import com.maxiaowei.pojo.vo.ItemInfoVO;
import com.maxiaowei.pojo.vo.NewItemsVO;
import com.maxiaowei.pojo.vo.ShopcartVO;
import com.maxiaowei.service.ItemService;
import com.maxiaowei.util.CustomJSONResult;
import com.maxiaowei.util.PagedGridResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 功能描述: 商品信息展示的相关接口
 * <p>
 * 作者: maxiaowei
 */
@RestController
@RequestMapping("items")
@Tag(name = "商品接口", description = "商品信息展示的相关接口")
@Slf4j
public class ItemsController extends BaseController {

    @Autowired
    private ItemService itemService;

    @Operation(summary = "查询商品详情", description = "查询商品详情", method = "GET")
    @GetMapping("/info/{itemId}")
    public CustomJSONResult info(
            @Parameter(name = "itemId", description = "商品id", required = true)
            @PathVariable String itemId) {
        if (StringUtils.isBlank(itemId)) {
            return CustomJSONResult.errorMsg(null);
        }
        Items item = itemService.queryItemById(itemId);
        List<ItemsImg> itemsImgList = itemService.queryItemImgList(itemId);
        List<ItemsSpec> itemsSpecList = itemService.queryItemSpecList(itemId);
        ItemsParam itemsParam = itemService.queryItemParam(itemId);
        ItemInfoVO itemInfoVO = new ItemInfoVO();
        itemInfoVO.setItem(item);
        itemInfoVO.setItemImgList(itemsImgList);
        itemInfoVO.setItemSpecList(itemsSpecList);
        itemInfoVO.setItemsParams(itemsParam);
        return CustomJSONResult.ok(itemInfoVO);
    }

    @Operation(summary = "查询商品评价等级", description = "查询商品评价等级", method = "GET")
    @GetMapping("/commentLevel")
    public CustomJSONResult commentLevel(
            @Parameter(name = "itemId", description = "商品id", required = true)
            @RequestParam String itemId) {
        if (StringUtils.isBlank(itemId)) {
            return CustomJSONResult.errorMsg(null);
        }
        CommentLevelCountVO countVO = itemService.queryCommentCounts(itemId);
        return CustomJSONResult.ok(countVO);
    }

    @Operation(summary = "查询商品评论", description = "查询商品评论", method = "GET")
    @GetMapping("/comments")
    public CustomJSONResult comments(
            @Parameter(name = "itemId", description = "商品id", required = true)
            @RequestParam String itemId,
            @Parameter(name = "level", description = "评价等级", required = false)
            @RequestParam Integer level,
            @Parameter(name = "page", description = "查询下一页的第几页", required = false)
            @RequestParam Integer page,
            @Parameter(name = "pageSize", description = "分页的每一页显示的条数", required = false)
            @RequestParam Integer pageSize) {
        if (itemId == null) {
            return CustomJSONResult.errorMsg(null);
        }
        if (page == null) {
            page = 1;
        }
        if (pageSize == null) {
            pageSize = COMMENT_PAGE_SIZE;
        }
        PagedGridResult result = itemService.queryPagedComments(itemId, level, page, pageSize);
        return CustomJSONResult.ok(result);
    }

    @Operation(summary = "搜索商品列表", description = "搜索商品列表", method = "GET")
    @GetMapping("/search")
    public CustomJSONResult search(
            @Parameter(name = "keywords", description = "关键字", required = true)
            @RequestParam String keywords,
            @Parameter(name = "sort", description = "排序规则", required = false)
            @RequestParam String sort,
            @Parameter(name = "page", description = "查询下一页的第几页", required = false)
            @RequestParam Integer page,
            @Parameter(name = "pageSize", description = "分页的每一页显示的条数", required = false)
            @RequestParam Integer pageSize) {
        if (StringUtils.isBlank(keywords)) {
            return CustomJSONResult.errorMsg(null);
        }
        if (page == null) {
            page = 1;
        }
        if (pageSize == null) {
            pageSize = PAGE_SIZE;
        }
        PagedGridResult result = itemService.searchItems(keywords, sort, page, pageSize);
        return CustomJSONResult.ok(result);
    }

    @Operation(summary = "通过分类id搜索商品列表", description = "通过分类id搜索商品列表", method = "GET")
    @GetMapping("/searchType")
    public CustomJSONResult searchType(
            @Parameter(name = "catId", description = "三级分类id", required = true)
            @RequestParam Integer catId,
            @Parameter(name = "sort", description = "排序规则", required = false)
            @RequestParam String sort,
            @Parameter(name = "page", description = "查询下一页的第几页", required = false)
            @RequestParam Integer page,
            @Parameter(name = "pageSize", description = "分页的每一页显示的条数", required = false)
            @RequestParam Integer pageSize) {
        if (catId == null) {
            return CustomJSONResult.errorMsg(null);
        }
        if (page == null) {
            page = 1;
        }
        if (pageSize == null) {
            pageSize = PAGE_SIZE;
        }
        PagedGridResult result = itemService.searchItems(catId, sort, page, pageSize);
        return CustomJSONResult.ok(result);
    }

    // 用于用户长时间未登录网站，刷新购物车中的数据（主要是商品价格），类似京东淘宝
    @Operation(summary = "根据商品规格ids查找最新的商品数据", description = "根据商品规格ids查找最新的商品数据", method = "GET")
    @GetMapping("/refresh")
    public CustomJSONResult refresh(
            @Parameter(name = "itemSpecIds", description = "拼接的规格ids", required = true, example = "1001,1003,1005")
            @RequestParam String itemSpecIds) {
        if (StringUtils.isBlank(itemSpecIds)) {
            return CustomJSONResult.ok();
        }
        List<ShopcartVO> result = itemService.queryItemsBySpecIds(itemSpecIds);
        return CustomJSONResult.ok(result);
    }
}
